import { CurrentUser, RealIp } from '@/decorators';
import {
  ActivityCreateDto,
  ActivityFindDto,
  ActivityListDto,
  ActivityLoadDto,
  ActivityOrderDto,
  ActivityQueryDto,
  ActivityRemoveDto,
  ActivityUpdateDto, TokenLogQueryDto
} from "@/dtos";
import { ActivityService } from '@/services/activity.service';
import { halt, ok } from '@/utils';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { Throttle, seconds } from '@nestjs/throttler';
import { BaseController } from './base.controller';

@Controller('activity')
export class ActivityController extends BaseController {
  constructor(private readonly activityService: ActivityService) {
    super();
  }

  @Post('order')
  @Throttle({
    short: {
      ttl: seconds(1),
      limit: 5,
    },
  })
  async order(
    @Body() dto: ActivityOrderDto,
    @CurrentUser('id') userId: number,
    @CurrentUser('mobile') mobile: string,
    @RealIp() ip: string,
  ) {
    return ok(await this.activityService.order(dto, userId, ip, mobile));
  }

  @Post('list')
  async getList(@Body() dto: ActivityListDto) {
    return ok(await this.activityService.getList(dto));
  }

  @Post('record/:type')
  async getRecord(
    @Body() dto: TokenLogQueryDto,
    @CurrentUser('id') uid: number,
    @Param('type') type: number,
  ) {
    return ok(await this.activityService.getRecord(dto, uid, type));
  }

  @Get('list')
  async getList2(@CurrentUser('id') userId: number) {
    return ok(await this.activityService.getList2(userId));
  }

  @Get('detail/:id')
  async getDetail(@Param('id') id: number, @CurrentUser('id') userId: number) {
    const { times } = await this.activityService.getTimes(id, userId);
    const data = await this.activityService.findOne(
      {
        id,
        isEnabled: true,
      },
      {
        id: true,
        title: true,
        image: true,
        startTime: true,
        endTime: true,
        perLimit: true,
        isSoldOut: true,
        burnPlans: true,
        points: true,
        prompt: true,
        content: true,
      },
    );
    if (!data) {
      return halt('数据异常');
    }
    return ok({
      ...data,
      times,
      points: Number(data.points ?? 0),
    });
  }

  @Post('admin/query')
  async query(@Body() dto: ActivityQueryDto) {
    return ok(await this.activityService.query(dto));
  }

  @Post('admin/find')
  async find(@Body() dto: ActivityFindDto) {
    return ok(await this.activityService.findOne(dto.where, dto.select));
  }

  @Post('admin/create')
  async create(@Body() dto: ActivityCreateDto) {
    return ok(await this.activityService.create(dto.data));
  }

  @Patch('admin/update')
  async update(@Body() dto: ActivityUpdateDto) {
    return ok(await this.activityService.update(dto));
  }

  @Delete('admin/remove')
  async remove(@Query() dto: ActivityRemoveDto) {
    return ok(await this.activityService.remove(dto));
  }

  @Post('admin/load')
  async load(@Body() dto: ActivityLoadDto) {
    return ok(await this.activityService.load(dto));
  }
}
